/*
* Copyright 2010 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/

package org.etherframework.core.util
{
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	import flash.utils.describeType;
	import flash.utils.getDefinitionByName;

	public final class ClassUtil
	{
		private var cache:Dictionary;
		
		public function ClassUtil(cache:Dictionary)
		{
			this.cache = cache;
		}
		
		public function getClass(obj:Object):Object
		{
			try {
				return (obj is String) ? getDefinitionByName(obj as String) : obj.constructor;
			}catch (e:*){}
			
			return null;
		}
		
		public function describeClassType(value:Object):XML
		{
			if (!value)
				return null;
			
			var className:String = getQualifiedClassName(value);
			
			if (cache[className])
				return cache[className]; 
			
			var clazz:Object = value.constructor;
			var descriptor:XML;
			
			if (clazz)
			{
				descriptor = describeType(clazz);
				cache[className] = descriptor;
				return descriptor;
			}
			
			return null;
		}
	}
}